1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.webmacro.util;
25
26 import org.webmacro.Log;
27
28 /***
29 * A log is a simple recipient of information about the system. It
30 * does not display or render this information, but instead passes
31 * it back to a set of log targets. A log is administered by a
32 * LogManager so you cannot instantiate one directly. Instead
33 * you must ask the Logmanager or a log instance.
34 */
35 public class LogSource implements Log
36 {
37
38 final private String _type;
39 final private String _description;
40 final private String _category;
41
42 /***
43 * The number of targets currently registered with this
44 * LogSource.
45 */
46 private int _tCount = 0;
47
48 final public boolean hasTargets ()
49 {
50 return (_tCount != 0);
51 }
52
53 final private LogTarget[][] _targets = new LogTarget[LogSystem.NONE][];
54
55 /***
56 * A LogSource must have a category and a type: called by Logsystem
57 */
58 LogSource (String category, String type, String description)
59 {
60 _category = category;
61 _type = type;
62 _description = description;
63 }
64
65 /***
66 * The type of this log source, as it would print in the log.
67 * For example "sys", or "log", or "wm". This type should be
68 * fairly short as it may be printed on every log line.
69 */
70 public String getType ()
71 {
72 return _type;
73 }
74
75 /***
76 * Get a description of this log source: what kind of messages
77 * does it contain. This can be a sentence or so of information
78 * about what kind of messages this log represents.
79 */
80 public String getDescription ()
81 {
82 return _description;
83 }
84
85 /***
86 * The category for this log source, as it woudl print in the
87 * log. This is the name which was passed to LogSystem to get
88 * the LogSystem instance from which this Log was created.
89 */
90 public String getCategory ()
91 {
92 return _category;
93 }
94
95 /***
96 * Explain myself
97 */
98 public String toString ()
99 {
100 return "LogSource(" + _category + "," + _type + "," + _description + ")";
101 }
102
103 /***
104 * Called by LogSystem to add a target
105 */
106 void addTarget (LogTarget t, int level)
107 {
108
109 if ((level < LogSystem.ALL) || (level > LogSystem.NONE))
110 {
111 error("Attempt to target with invalid log level: " + level);
112 return;
113 }
114
115 LogTarget[] ts = _targets[level];
116 if (ts == null)
117 {
118 ts = new LogTarget[1];
119 ts[0] = t;
120 _targets[level] = ts;
121 _tCount++;
122 return;
123 }
124
125
126 for (int i = 0; i < ts.length; i++)
127 {
128 if (ts[i] == t)
129 {
130 return;
131 }
132 }
133
134
135 LogTarget[] nts = new LogTarget[ts.length + 1];
136 System.arraycopy(ts, 0, nts, 0, ts.length);
137 nts[ts.length] = t;
138 _targets[level] = nts;
139 _tCount++;
140 }
141
142 /***
143 * Remove a target from a specific list
144 */
145 void removeTarget (LogTarget t, int level)
146 {
147 LogTarget[] ts = _targets[level];
148 if (ts == null)
149 {
150 return;
151 }
152 boolean match = false;
153 for (int i = 0; i < ts.length; i++)
154 {
155 if (ts[i] == t)
156 {
157 match = true;
158 }
159 }
160 if (!match)
161 {
162 return;
163 }
164 _tCount--;
165 if (ts.length == 1)
166 {
167 _targets[level] = null;
168 }
169
170 LogTarget[] nts = new LogTarget[ts.length - 1];
171 int pos = 0;
172 for (int i = 0; i < ts.length; i++)
173 {
174 if (ts[i] != t)
175 {
176 nts[pos++] = ts[i];
177 }
178 }
179 _targets[level] = nts;
180 }
181
182 /***
183 * Debug messages are incidental programmer notes which should
184 * not be enabled in a production system. They are useful only
185 * during development.
186 */
187 public void debug (String msg, Throwable e)
188 {
189 log(LogSystem.DEBUG, msg, e);
190 }
191
192 /***
193 * A shortform for debug(msg,null)
194 */
195 public void debug (String msg)
196 {
197 log(LogSystem.DEBUG, msg, null);
198 }
199
200 /***
201 * Info is fairly unimportant information about routine processing
202 * within the system. They may be interesting on a production
203 * system, but also can typically be ignored.
204 */
205 public void info (String msg)
206 {
207 log(LogSystem.INFO, msg, null);
208 }
209
210 /***
211 * Notices are important information about routine processing
212 * within the system. For example, startup and shutdown messages.
213 * They are likely interesting to people running a production
214 * system since they provide timestamps for important events.
215 */
216 public void notice (String msg)
217 {
218 log(LogSystem.NOTICE, msg, null);
219 }
220
221 /***
222 * Warnings are messages outlining unexpected non-routine events
223 * within the system. They may indicate larger problems, but in
224 * and of themselves refer to problems the system is capable of
225 * handling on its own. On a correctly functioning production
226 * system you would expect to see only a few warnings.
227 */
228 public void warning (String msg, Throwable e)
229 {
230 log(LogSystem.WARNING, msg, e);
231 }
232
233 /***
234 * A shortform for warning(msg,null)
235 */
236 public void warning (String msg)
237 {
238 log(LogSystem.WARNING, msg, null);
239 }
240
241 /***
242 * A shortform for error(msg,null)
243 */
244 public void error (String msg)
245 {
246 log(LogSystem.ERROR, msg, null);
247 }
248
249 /***
250 * An error is a major failure within the system. Typically it is
251 * something which cannot easily be handled by the system. On a
252 * correctly functioning production system you would not expect
253 * to see any error messages.
254 */
255 public void error (String msg, Throwable e)
256 {
257 log(LogSystem.ERROR, msg, e);
258 }
259
260
261 public boolean loggingDebug ()
262 {
263 return (_targets[LogSystem.DEBUG] != null);
264 }
265
266 public boolean loggingInfo ()
267 {
268 return (_targets[LogSystem.INFO] != null);
269 }
270
271 public boolean loggingNotice ()
272 {
273 return (_targets[LogSystem.NOTICE] != null);
274 }
275
276 public boolean loggingWarning ()
277 {
278 return (_targets[LogSystem.WARNING] != null);
279 }
280
281 protected void log (int level, String msg, Throwable e)
282 {
283 LogTarget[] targets = _targets[level];
284 if (targets == null)
285 {
286 return;
287 }
288 String sLevel = LogSystem.LEVELS[level];
289 java.util.Date date = Clock.getDate();
290 boolean flush = (level >= LogSystem.NOTICE);
291 for (int i = 0; i < targets.length; i++)
292 {
293 targets[i].log(date, _type, sLevel, msg, e);
294 if (flush) targets[i].flush();
295 }
296 }
297 }
298
299